package classconverstation.cscl.ist.psu.edu.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import classconverstation.cscl.ist.psu.edu.client.messengers.User;

public class UserChecker {

	
	static public User getUser(String uid){
		try {
			Connection conn = DBConnector.getInstance().getConnection();
			String sql = "SELECT * from " + LoginServiceImpl.CLASS_CONVERSATION_DB + ".user WHERE uid = ?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, uid);

			ResultSet result = ps.executeQuery();
			if (result.next()) {
				String fname = result.getString("fname");
				String lname = result.getString("lname");
				String pid = result.getString("psuid");
				int utype = result.getInt("utype");
				User user = new User(uid, fname,lname,pid, utype);
				result.close();
				ps.close();
				conn.close();
				
				return user;
			}
			else{
				System.out.println("returning null");
				return null;
			}
		}
		catch (SQLException sqle) {
			System.out.println(sqle.getMessage());
			sqle.printStackTrace();
			return null;
		}
	}
	
	static public boolean activateUser(String psuid, String activationCode) throws SQLException {
	    Connection conn = DBConnector.getInstance().getConnection();
        String sql = "UPDATE " + LoginServiceImpl.CLASS_CONVERSATION_DB + ".user set activated = 1 WHERE psuid = ? and activation_code = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, psuid);
        ps.setString(2, activationCode);
        ps.execute();
        
        sql = "SELECT activated from user where psuid = ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, psuid);
        ResultSet rs = ps.executeQuery();
        if (rs.first()) {
            rs.beforeFirst();
            rs.next();
            if (rs.getInt("activated") == 0) {
                rs.close();
                rs = null;
                ps.close();
                ps = null;
                conn.close();
                conn = null;
                return false;
            }
            else {
                rs.close();
                rs = null;
                ps.close();
                ps = null;
                conn.close();
                conn = null;
                return true;
            }
        }
        return false;
	}
	
	static public boolean isUserActivated(String uid){
	        try {
	            Connection conn = DBConnector.getInstance().getConnection();
	            String sql = "SELECT * from " + LoginServiceImpl.CLASS_CONVERSATION_DB + ".user WHERE uid = ?";
	            PreparedStatement ps = conn.prepareStatement(sql);
	            ps.setString(1, uid);

	            ResultSet result = ps.executeQuery();
	            if (result.next()) {
	                if (result.getInt("activated") == 0) {
	                    return false;
	                }
	                else return true;
	            }
	            else{
	                System.out.println("returning null");
	                return false;
	            }
	        }
	        catch (SQLException sqle) {
	            System.out.println(sqle.getMessage());
	            sqle.printStackTrace();
	            return false;
	        }
	    }
}
